home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / GNUPLOTsrc.lha / term / pbm.trm < prev    next >
Encoding:
Text File  |  1996-01-22  |  9.4 KB  |  373 lines

  1. /*
  2.  * $Id: pbm.trm,v 1.9 1995/12/20 21:48:03 drd Exp $
  3.  *
  4.  */
  5.  
  6. /* GNUPLOT - pbm.trm */
  7. /*
  8.  * Copyright (C) 1990 - 1993   
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software  is provided "as is" without express or implied warranty.
  21.  * 
  22.  * This file is included by ../term.c.
  23.  *
  24.  * This terminal driver supports:
  25.  *  pbm
  26.  *
  27.  * AUTHORS
  28.  *  Russell Lang
  29.  *
  30.  * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
  31.  * 
  32.  */
  33.  
  34. /* The following pbmplus drivers use the generic bit mapped graphics
  35.    routines from bitmap.c to build up a bit map in memory.  The driver
  36.    interchanges colomns and lines in order to access entire lines
  37.    easily and returns the lines to get bits in the right order :
  38.    (x,y) -> (y,XMAX-1-x). */
  39. /* This interchange is done by calling b_makebitmap() with reversed 
  40.    xmax and ymax, and then setting b_rastermode to TRUE.  b_setpixel()
  41.    will then perform the interchange before each pixel is plotted */
  42. /* See Jef Poskanzer's excellent PBMplus package for more details of
  43.    the Portable BitMap format and for programs to convert PBM files  
  44.    to other bitmap formats. */
  45.  
  46. #ifndef GOT_DRIVER_H
  47. #include "driver.h"
  48. #endif
  49.  
  50. #ifdef TERM_REGISTER
  51. register_term(pbm_driver)
  52. #endif
  53.  
  54. #ifdef TERM_PROTO
  55. TERM_PUBLIC void PBMoptions __P((void));
  56. TERM_PUBLIC void PBMinit __P((void));
  57. TERM_PUBLIC void PBMreset __P((void));
  58. TERM_PUBLIC void PBMsetfont __P((void));
  59. TERM_PUBLIC void PBMgraphics __P((void));
  60. TERM_PUBLIC void PBMmonotext __P((void));
  61. TERM_PUBLIC void PBMgraytext __P((void));
  62. TERM_PUBLIC void PBMcolortext __P((void));
  63. TERM_PUBLIC void PBMtext __P((void));
  64. TERM_PUBLIC void PBMlinetype __P((int linetype));
  65. TERM_PUBLIC void PBMpoint __P((unsigned int x, unsigned int y, int point));
  66. #endif /* TERM_PROTO */
  67.  
  68. #ifdef TERM_BODY
  69.  
  70. /* make XMAX and YMAX a multiple of 8 */
  71. #define PBM_XMAX (640)
  72. #define PBM_YMAX (480)
  73. #define PBM_VCHAR (FNT5X9_VCHAR)
  74. #define PBM_HCHAR (FNT5X9_VCHAR)
  75. #define PBM_VTIC FNT5X9_HBITS
  76. #define PBM_HTIC FNT5X9_HBITS
  77.  
  78. static int pbm_font=1;    /* small font */
  79. static int pbm_mode=0;  /* 0:monochrome 1:gray 2:color */
  80.  
  81. /* 7=black, 0=white */
  82. static int pgm_gray[]={7,1,6,5,4,3,2,1,7};  /* grays  */
  83. /* bit3=!intensify, bit2=!red, bit1=!green, bit0=!blue */
  84. static int ppm_color[]={15,8,3,5,6,4,2,1,11,13,14}; /* colors */
  85.  
  86. TERM_PUBLIC void PBMoptions()
  87. {
  88.   pbm_font=1;
  89.   pbm_mode=0;
  90.  
  91.   term_options[0]='\0';
  92.  
  93.   while (!END_OF_COMMAND) {
  94.     if (almost_equals(c_token,"s$mall"))
  95.       pbm_font=1;
  96.     else if (almost_equals(c_token,"me$dium"))
  97.       pbm_font=2;
  98.     else if (almost_equals(c_token,"l$arge"))
  99.       pbm_font=3;
  100.     else if (almost_equals(c_token,"mo$nochrome"))
  101.       pbm_mode=0;
  102.     else if (almost_equals(c_token,"g$ray"))
  103.       pbm_mode=1;
  104.     else if (almost_equals(c_token,"c$olor"))
  105.       pbm_mode=2;
  106.     else {
  107.       pbm_font=1; /* reset to default, since term is already set */
  108.       pbm_mode=0;
  109.       int_error("expecting: {small, medium, large} and {monochrome, gray, color}",c_token);
  110.     }
  111.     c_token++;
  112.   }
  113.  
  114.   /* setup options string */
  115.  
  116.   switch(pbm_font) {
  117.     case 1: strcat(term_options,"small"); break;
  118.     case 2: strcat(term_options,"medium"); break;
  119.     case 3: strcat(term_options,"large"); break;
  120.   }
  121.  
  122.   switch(pbm_mode) {
  123.     case 0: strcat(term_options," monochrome"); break;
  124.     case 1: strcat(term_options," gray"); break;
  125.     case 2: strcat(term_options," color"); break;
  126.   }
  127. }
  128.  
  129.  
  130. TERM_PUBLIC void PBMinit()
  131. {
  132. #ifdef REOPEN_BINARY
  133.    reopen_binary();
  134. #endif /* REOPEN_BINARY */
  135. }
  136.  
  137.  
  138. TERM_PUBLIC void PBMreset()
  139. {
  140. #ifdef vms
  141.    fflush_binary();
  142. #endif /* vms */
  143. }
  144.  
  145.  
  146. TERM_PUBLIC void PBMsetfont()
  147. {
  148.     switch(pbm_font) {
  149.         case 1:
  150.             b_charsize(FNT5X9);
  151.             term->v_char = FNT5X9_VCHAR;
  152.             term->h_char = FNT5X9_HCHAR;
  153.             term->v_tic = FNT5X9_HBITS;
  154.             term->h_tic = FNT5X9_HBITS;
  155.             break;
  156.         case 2:
  157.             b_charsize(FNT9X17);
  158.             term->v_char = FNT9X17_VCHAR;
  159.             term->h_char = FNT9X17_HCHAR;
  160.             term->v_tic = FNT9X17_HBITS;
  161.             term->h_tic = FNT9X17_HBITS;
  162.             break;
  163.         case 3:
  164.             b_charsize(FNT13X25);
  165.             term->v_char = FNT13X25_VCHAR;
  166.             term->h_char = FNT13X25_HCHAR;
  167.             term->v_tic = FNT13X25_HBITS;
  168.             term->h_tic = FNT13X25_HBITS;
  169.             break;
  170.     }
  171. }
  172.  
  173.  
  174. TERM_PUBLIC void PBMgraphics()
  175. {
  176.   int numplanes=1;
  177.  
  178.   switch(pbm_mode) {
  179.     case 1: numplanes=3; break;
  180.     case 2: numplanes=4; break;
  181.   }
  182.  
  183.   PBMsetfont();
  184.   /* rotate plot -90 degrees by reversing XMAX and YMAX and by 
  185.   setting b_rastermode to TRUE */
  186.   b_makebitmap((unsigned int)(PBM_YMAX*ysize),
  187.                (unsigned int)(PBM_XMAX*xsize),numplanes);
  188.   b_rastermode = TRUE;
  189.  
  190.   if(pbm_mode!=0)
  191.     b_setlinetype(0); /* solid lines */
  192. }
  193.  
  194.  
  195. static void PBMmonotext()
  196. {
  197.   register int x,j,row;
  198.  
  199.    fprintf(outfile,"P4\n");
  200.    fprintf(outfile,"%u %u\n", b_ysize, b_xsize);
  201.  
  202.    /* dump bitmap in raster mode */
  203.    for (x = b_xsize-1; x >= 0; x--) {
  204.       row = (b_ysize/8)-1;
  205.       for (j = row; j >= 0; j--) {
  206.          (void) fputc( (char)(*((*b_p)[j]+x)), outfile );
  207.       }
  208.    }
  209.  
  210.    b_freebitmap();
  211. }
  212.  
  213. static void PBMgraytext()
  214. {
  215.   register int x,j,row;
  216.   register int i,value;
  217.   int mask, plane1, plane2, plane3;
  218.  
  219.    fprintf(outfile,"P5\n");
  220.    fprintf(outfile,"%u %u\n", b_ysize, b_xsize);
  221.    fprintf(outfile,"%u\n",7);
  222.  
  223.    /* dump bitmap in raster mode */
  224.    for (x = b_xsize-1; x >= 0; x--) {
  225.       row = (b_ysize/8)-1;
  226.       for (j = row; j >= 0; j--) {
  227.          mask = 0x80;
  228.          plane1=(*((*b_p)[j]+x));
  229.          plane2=(*((*b_p)[j+b_psize]+x));
  230.          plane3=(*((*b_p)[j+b_psize+b_psize]+x));
  231.          for (i=0; i<8; i++) {
  232.             value=7;
  233.             if (plane1 & mask)  value-=1;
  234.             if (plane2 & mask)  value-=2;
  235.             if (plane3 & mask)  value-=4;
  236.             (void) fputc( (char)(value), outfile );
  237.             mask>>=1;
  238.          }
  239.       }
  240.    }
  241.  
  242.    b_freebitmap();
  243. }
  244.  
  245. static void PBMcolortext()
  246. {
  247.   register int x,j,row;
  248.   register int i;
  249.   int mask, plane1, plane2, plane3, plane4;
  250.   int red, green, blue;
  251.  
  252.    fprintf(outfile,"P6\n");
  253.    fprintf(outfile,"%u %u\n", b_ysize, b_xsize);
  254.    fprintf(outfile,"%u\n",3);
  255.  
  256.    /* dump bitmap in raster mode */
  257.    for (x = b_xsize-1; x >= 0; x--) {
  258.       row = (b_ysize/8)-1;
  259.       for (j = row; j >= 0; j--) {
  260.          mask = 0x80;
  261.          plane1=(*((*b_p)[j]+x));
  262.          plane2=(*((*b_p)[j+b_psize]+x));
  263.          plane3=(*((*b_p)[j+b_psize+b_psize]+x));
  264.          plane4=(*((*b_p)[j+b_psize+b_psize+b_psize]+x));
  265.          for (i=0; i<8; i++) {
  266.             red = (plane3 & mask) ? 1 : 3;
  267.             green = (plane2 & mask) ? 1 : 3;
  268.             blue = (plane1 & mask) ? 1 : 3;
  269.             if (plane4 & mask) {
  270.                red--; green--; blue--;
  271.             }
  272.             (void) fputc( (char)(red), outfile );
  273.             (void) fputc( (char)(green), outfile );
  274.             (void) fputc( (char)(blue), outfile );
  275.             mask>>=1;
  276.          }
  277.       }
  278.    }
  279.  
  280.    b_freebitmap();
  281. }
  282.  
  283. TERM_PUBLIC void PBMtext()
  284. {
  285.   switch(pbm_mode) {
  286.     case 0: PBMmonotext(); break;
  287.     case 1: PBMgraytext(); break;
  288.     case 2: PBMcolortext(); break;
  289.   }
  290. }
  291.  
  292.  
  293. TERM_PUBLIC void PBMlinetype(linetype)
  294. int linetype;
  295. {
  296.   switch(pbm_mode) {
  297.     case 0:
  298.       b_setlinetype(linetype);
  299.     break;
  300.     case 1:
  301.       if (linetype>=7)
  302.         linetype %= 7;
  303.       b_setvalue(pgm_gray[linetype+2]);
  304.     break;
  305.     case 2:
  306.       if (linetype>=9)
  307.         linetype %= 9;
  308.       b_setvalue(ppm_color[linetype+2]);
  309.     break;
  310.   }
  311. }
  312.  
  313. TERM_PUBLIC void PBMpoint(x,y,point)
  314. unsigned int x,y;
  315. int point;
  316. {
  317.   if(pbm_mode==0) line_and_point(x,y,point);
  318.   else            do_point(x,y,point);
  319. }
  320.  
  321. #endif /* TERM_BODY */
  322.  
  323. #ifdef TERM_TABLE
  324.  
  325. #define PBMmove b_move
  326. #define PBMvector b_vector
  327. #define PBMtext_angle b_text_angle
  328. #define PBMput_text b_put_text
  329.  
  330. TERM_TABLE_START(pbm_driver)
  331.       "pbm", "Portable bitmap [small medium large] [monochrome gray color]",
  332.        PBM_XMAX, PBM_YMAX, PBM_VCHAR,
  333.        PBM_HCHAR, PBM_VTIC, PBM_HTIC, PBMoptions,
  334.        PBMinit, PBMreset, PBMtext, null_scale,
  335.        PBMgraphics, PBMmove, PBMvector, PBMlinetype,
  336.        PBMput_text, PBMtext_angle, null_justify_text, PBMpoint,
  337.        do_arrow, set_font_null,
  338.             0, /* pointsize */
  339.             TERM_CAN_MULTIPLOT
  340. TERM_TABLE_END(pbm_driver)
  341.  
  342. #undef LAST_TERM
  343. #define LAST_TERM pbm_driver
  344.  
  345. #endif /* TERM_TABLE */
  346.  
  347.  
  348. #ifdef TERM_HELP
  349. START_HELP(pbm)
  350. "1 pbm",
  351. "?set terminal pbm",
  352. "?pbm",
  353. " Several options may be set in the PBMplus driver.",
  354. "",
  355. " Syntax:",
  356. "         set terminal pbm {<fontsize>} {<colormode>}",
  357. "",
  358. " where <fontsize> is `small`, `medium`, or `large` and <colormode> is",
  359. " `monochrome`, `gray` or `color`.  Default size is 640 pixels wide and 480",
  360. " pixels high.  The output for `monochrome` is a portable bitmap (one bit per",
  361. " pixel).  The output for `gray` is a portable graymap (three bits per pixel).",
  362. " The output for `color` is a portable pixmap (color, four bits per pixel).",
  363. " The output of these drivers can be used with Jef Poskanzer's excellent",
  364. " PBMPLUS package, which provides programs to convert the above PBMPLUS formats",
  365. " to GIF, TIFF, MacPaint, Macintosh PICT, PCX, X11 bitmap and many others.",
  366. "",
  367. " Examples:",
  368. "         set term pbm small",
  369. "         set size 2,2",
  370. "         set term pbm color medium"
  371. END_HELP(pbm)
  372. #endif
  373.